// pages/index/index.js
import Config from "../../main.config";
import {getUserInfo} from "../../service/Common";
import {getStorageSync} from "../../utils/Storage";
import {getRegisterDetail as getEnterpriseRegisterDetail} from "../../service/Enterprise";
import {getRegisterDetail as getPersonnelRegisterDetail} from "../../service/Personnel";
import {getRegisterDetail as getStudentRegisterDetail} from "../../service/Student"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageShow: false, //页面展示
        userType: -1, //用户类型
        active: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let active = parseInt(options.active || 0);
        active >= 0 ? this.setData({active: active}) : null;
        this.getUserInfo()
    },

    /*
    * 获取用户信息
    * 如果用户信息存在 且 roleId存在 则该用户已注册
    * 如果不存在 则跳转注册页面进行注册
    * */
    getUserInfo: async function () {
        try {
            let openId = getStorageSync(Config.storage.openId);
            let userInfo = await getUserInfo(openId);
            let roleId = userInfo && userInfo.roleId ? parseInt(userInfo.roleId) : "";
            this.data._userInfo = userInfo;
            if (roleId) {
                await this._userStatusChecked(userInfo.userId, roleId);
            } else {
                wx.redirectTo({url: "/pages/login/index/index"});
            }
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 用户状态监测
    * 如果 用户时企业类型 需要审核
    * status === 1审核中; 跳转提示页面
    * status === 2审核通过 展示页面信息;
    * status === 3审核不通过 跳转提示页面
    * 如果是其它用户
    * 判断缓存token是否存在 如果存在 则直接展示对应的内容 如果不存在 则跳转登录页面
    * */
    _userStatusChecked: async function (userId, roleId) {
        let token = getStorageSync(Config.storage.token);
        let detail = {};
        switch (roleId) {
            case 6: //企业用户
                detail = await getEnterpriseRegisterDetail({createUser: userId});
                if (detail.status !== 2) { //未通过审核
                    wx.redirectTo({url: "/packageEnterprise/pages/status/index"})
                } else if (!token) {
                    wx.redirectTo({url: "/pages/login/index/index"})
                } else {
                    this.setData({
                        pageShow: true,
                        userType: roleId
                    });
                }
                break
            case 7:
                detail = await getPersonnelRegisterDetail(userId);
                if (!token) {
                    wx.redirectTo({url: "/pages/login/index/index"});
                } else {
                    if (!detail.schoolName) {
                        wx.redirectTo({url: "/packagePersonnel/pages/education/index?todo=create"})
                    } else {
                        this.setData({
                            pageShow: true,
                            userType: roleId
                        });
                    }
                }
                break
            case 8:
                detail = await getStudentRegisterDetail(userId);
                if (!token) {
                    wx.redirectTo({url: "/pages/login/index/index"});
                } else {
                    if (!detail.schoolName) {
                        wx.redirectTo({url: "/packageStudent/pages/education/index?todo=create"})
                    } else {
                        this.setData({
                            pageShow: true,
                            userType: roleId
                        });
                    }
                }
                break
            default:
                break
        }
        this.data._registerInfo = detail;
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        let userInfo = this.data._userInfo;
        let roleId = parseInt(userInfo.roleId);
        let registerInfo = this.data._registerInfo;
        if (roleId === 6 || roleId === 7) { //企业用户 分享邀请员工
            return {
                title: "小伙伴们，快来加入这个大家庭吧！",
                path: "/pages/login/index/index?companyId=" + (registerInfo.companyId || registerInfo.id)
                + "&companyName=" + (registerInfo.companyName || registerInfo.name)
                + "&createUser=" + registerInfo.createUser,
                imageUrl: "/images/share.jpg"
            }
        } else {
            return {
                title: "小伙伴们，快来加入这个大家庭吧！",
                path: "/pages/login/index/index?createUser=" + registerInfo.createUser,
                imageUrl: "/images/share.jpg"
            }
        }
    }
})